package com.honeywell.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.honeywell.entity.Camera;

/**
 * @author: mabingbing
 * @date: 2018年4月27日
 * @descript:
 */
@RestController
public class CacheController {

	@Autowired
	CacheManager cacheManager;

	@RequestMapping(value="/addElement",method=RequestMethod.GET)
	public String addElement(@RequestParam("id") String id,
			@RequestParam("id") String name, @RequestParam("id") String type) {
		
		Cache cache = cacheManager.getCache("IotServerCache");
		Camera camera = new Camera(id, name, type);
		Element element = new Element(id.toString(), camera); 
		cache.put(element);
		return "success";
		
	}
	
	@RequestMapping(value="/getElements",method=RequestMethod.GET)
	public Map<String, Object> getElements() {
		
		Cache cache = cacheManager.getCache("IotServerCache");
		List<String> keys = cache.getKeys();
		Map<String, Object> results = new HashMap<>();
		for(String key : keys){
			results.put(key, cache.get(key));
		}
		return results;
		
	}
}
